package com.maiji.cloud.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.util.StringUtils;

/**
 * Description:
 * 
 * @author zhanglr
 * @date 2019年6月13日
 */
public class TimeUtil {

	/**
	 * 获得当天零点零分零秒时间
	 * 
	 * @return
	 */
	public static Date getZeroTime(Date time) {
		Calendar calendar = Calendar.getInstance();

		if (time == null)
			calendar.setTime(new Date());
		else
			calendar.setTime(time);

		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date zero = calendar.getTime();
		return zero;
	}

	/**
	 * 获得昨天天零点零分零秒时间
	 * 
	 * @return
	 */
	public static Date getBefordayTime(Date time, int day, int type) {
		Calendar calendar = Calendar.getInstance();
		if (time == null)
			calendar.setTime(new Date());
		else
			calendar.setTime(time);

		if (type == 0) {
			calendar.add(Calendar.DATE, +day);
		} else {
			calendar.add(Calendar.DATE, -day);
		}
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();

	}
	/**
	 * 几天前的时间或几天后的时间 
	 * 
	 * @return
	 */
	public static Date getDateTime(Date time, int hours, int type) {
		Long times = time.getTime();
		if (type == 0) {
			times = time.getTime() + (hours * 60 * 60 * 1000);
		} else {
			times = time.getTime() - (hours * 60 * 60 * 1000);
		}
		
		return  new Date(times);
		
	}

	/**
	 * 格式化日期
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static String date2String(Date date, String pattern) throws Exception {

		if (date == null) {
			return null;
		}

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
				StringUtils.isEmpty(pattern) ? "yyyy-MM-dd HH:mm:ss" : pattern);

		return simpleDateFormat.format(date);
	}

	/**
	 * 时间格式字符串转date
	 * 
	 * @param time
	 * @param pattern
	 * @return
	 * @throws Exception
	 */
	public static Date stringToDate(String time, String pattern) throws Exception {

		if (StringUtils.isEmpty(time)) {
			return null;
		}

		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern == null ? "yyyy-MM-dd HH:mm:ss" : pattern);

		Date date = simpleDateFormat.parse(time);

		return date;
	}

	/*
	 * 将时间转换为时间戳
	 */
	public static Long getCurDateStamp() throws Exception {

		Date date = new Date();
		long ts = date.getTime();
		return ts;
	}
	
	/**
	 * 计算两日期相差的天数
	 * @param smdate
	 * @param bdate
	 * @return
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate,Date bdate) throws ParseException

	{

	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

	smdate=sdf.parse(sdf.format(smdate));

	bdate=sdf.parse(sdf.format(bdate));

	Calendar cal = Calendar.getInstance();

	cal.setTime(smdate);

	long time1 = cal.getTimeInMillis();

	cal.setTime(bdate);

	long time2 = cal.getTimeInMillis();

	long between_days=(time2-time1)/(1000*3600*24);

	return Integer.parseInt(String.valueOf(between_days));

	}
	

	public static void main(String[] args) throws Exception {

		/*
		 * //获取系统现在的时间 String now = "2019-6-30 00:05:30"; //获取入库时间 String rukuTime =
		 * "2019-06-31 23:05:30";
		 * 
		 * SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd"); Date
		 * endDate = format.parse(rukuTime); Date startDate = format.parse(now);
		 * 
		 * long day=(endDate.getTime()-rukuTime.getTime())/(24*60*60*1000);
		 * System.out.println(day);
		 */
//		 long day=(new Date().getTime()-stringToDate("2019-07-01 12:01:31",null).getTime())/(24*60*60*1000);
		 System.out.println(daysBetween((getBefordayTime(new Date(),378,-2)),new Date()));
		
	}
}
